package com.gitee.ixtf.vertx.kotlinx

import io.vertx.core.eventbus.EventBus
import io.vertx.core.json.JsonObject
import io.vertx.kotlin.core.json.jsonObjectOf
import kotlinx.coroutines.reactor.mono

fun sockJsMessage(type: String, payload: Any? = null) =
    jsonObjectOf(
        "type" to type,
        "payload" to
            when (payload) {
              null -> null
              is String -> payload
              else -> JsonObject.mapFrom(payload)
            })

fun EventBus.sendSockJs(address: String, type: String, payload: Any? = null) {
  mono { send(address, sockJsMessage(type, payload)) }.subscribe()
}

fun EventBus.publishSockJs(address: String, type: String, payload: Any? = null) {
  mono { publish(address, sockJsMessage(type, payload)) }.subscribe()
}
